/**
 * 作者: wgx
 * 日期: 2025-03-25 14:25:51
 * 备注:  用户观看视频激励奖励信息
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserAdvideo;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserAdvideoModel extends BaseModel implements Model {
    public static final String LISTKEY = "";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // 用户ID
    protected long resetTime; // 每日重置时间
    protected String adCode = ""; // 唯一码
    protected long adStartTime; // 视频开始观看时间
    protected String dayInfo = ""; // 每日广告观看次数 ({"id":num})

    public void reset() {
    }

    protected String getKey() {
        return "UserAdvideo." + id;
    }

    protected String getListKey() {
        return null;
    }

    protected String getListItemKey() {
        return null;
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_advideo"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, id, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, id, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // 用户ID
        obj.put("resetTime", resetTime); // 每日重置时间
        obj.put("adCode", adCode); // 唯一码
        obj.put("adStartTime", adStartTime); // 视频开始观看时间
        obj.put("dayInfo", dayInfo); // 每日广告观看次数 ({"id":num})
        return obj;
    }

    public PBUserAdvideo.Builder takeInitBuilder() {
        PBUserAdvideo.Builder sb = PBUserAdvideo.newBuilder();
        if (!adCode.isEmpty()) {
            sb.setAdCode(adCode);
        }
        if (adStartTime != 0) {
            sb.setAdStartTime(adStartTime);
        }
        if (!dayInfo.isEmpty()) {
            sb.setDayInfo(dayInfo);
        }
        return sb;
    }

    public PBUserAdvideo.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserAdvideo.Builder sb = PBUserAdvideo.newBuilder();
        if (isFieldUpdate("adCode")) {
            sb.setAdCode(adCode);
        }
        if (isFieldUpdate("adStartTime")) {
            sb.setAdStartTime(adStartTime);
        }
        if (isFieldUpdate("dayInfo")) {
            sb.setDayInfo(dayInfo);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getResetTime() {
        return resetTime;
    }

    public void putResetTime(long resetTime) {
        if (this.resetTime != resetTime) {
            this.resetTime = resetTime;
            updateRedisHashField(getKey(), "resetTime", this.resetTime);
        }
    }

    public void addResetTime(long value) {
        this.resetTime += value;
        updateRedisHashField(getKey(), "resetTime", this.resetTime);
    }

    public String getAdCode() {
        return adCode;
    }

    public void putAdCode(String adCode) {
        if (!this.adCode.equals(adCode)) {
            this.adCode = adCode;
            updateRedisHashField(getKey(), "adCode", this.adCode);
        }
    }

    public long getAdStartTime() {
        return adStartTime;
    }

    public void putAdStartTime(long adStartTime) {
        if (this.adStartTime != adStartTime) {
            this.adStartTime = adStartTime;
            updateRedisHashField(getKey(), "adStartTime", this.adStartTime);
        }
    }

    public void addAdStartTime(long value) {
        this.adStartTime += value;
        updateRedisHashField(getKey(), "adStartTime", this.adStartTime);
    }

    public String getDayInfo() {
        return dayInfo;
    }

    public void putDayInfo(String dayInfo) {
        if (!this.dayInfo.equals(dayInfo)) {
            this.dayInfo = dayInfo;
            updateRedisHashField(getKey(), "dayInfo", this.dayInfo);
        }
    }

}